home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / plan / src / file_w.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  6KB  |  213 lines

  1. /*
  2.  * write the master list to the ~/.schedule file.
  3.  *
  4.  *    write_database(list, path, what)    Write appointments and config
  5.  *                        info to a database file.
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <time.h>
  10. #include <Xm/Xm.h>
  11. #ifdef MIPS
  12. #include <sys/types.h>
  13. #include <sys/fcntl.h>
  14. #else
  15. #include <unistd.h>
  16. #include <fcntl.h>
  17. #endif
  18. #include <sys/stat.h>
  19. #include "cal.h"
  20.  
  21. extern struct tm    *time_to_tm();
  22. extern char        *resolve_tilde();    /* evaluates path with ~ */
  23. extern int        errno;
  24. extern char        *progname;        /* argv[0] */
  25. extern struct config    config;            /* global configuration data */
  26. extern Widget        mainwindow;        /* for error popup */
  27. extern struct user    *user;            /* user list for week view */
  28. extern int        nusers;            /* # of users in user list */
  29. extern char        Print_Spooler[100];    /* print spooling command */
  30.  
  31.  
  32. /*
  33.  * write a list to a file. The <what> bitmap determines what is written:
  34.  * WR_CONFIG writes configuration info, WR_PUBLIC writes public appointments,
  35.  * and WR_PRIVATE writes private appointments. Specifying WR_PRIVATE has the
  36.  * side effect of making the file read-only. After calling this routine,
  37.  * call resynchronize_daemon() to tell the daemon to re-read the databases.
  38.  * Returns FALSE if the file could not be written.
  39.  */
  40.  
  41. write_database(list, path, what)
  42.     struct list        *list;        /* list to write */
  43.     char            *path;        /* file to write list to */
  44.     int            what;        /* bitmap of WR_* macros */
  45. {
  46.     register struct config    *c = &config;
  47.     FILE            *fp;        /* open file */
  48.     char            line[1024];    /* line buffer */
  49.     register char        *p, *q;        /* string copy pointers */
  50.     register struct entry    *ep;        /* list entry being written */
  51.     int            i;        /* entry counter */
  52.     int            perm;        /* file permissions */
  53.     struct tm        *tm;        /* time to m/d/y h:m:s conv */
  54.  
  55.     list->locked = TRUE;
  56.     path = resolve_tilde(path);
  57.  
  58.     /* Backup Database... */
  59.     sprintf(line, "%s.bak", path);
  60.     link(path, line); unlink(path);
  61.  
  62.     if (!(fp = fopen(path, "w"))) {
  63.         create_error_popup(mainwindow, errno,
  64.             "Failed to create appointment database %s", path);
  65.         list->locked = FALSE;
  66.         return(FALSE);
  67.     }
  68.  
  69.     perm = what & WR_PRIVATE ? 0600 : 0644;
  70. #if 0                    /* SCO has no fchmod */
  71.     if (fchmod(fileno(fp), perm))
  72. #else
  73.     if (chmod(path, perm))
  74. #endif
  75.         create_error_popup(mainwindow, errno,
  76.               "Cannot chmod 0%o appointment database %s.", perm, path);
  77.     lockfile(fp, TRUE);
  78.     if (what & WR_CONFIG) {
  79.         fprintf(fp, "o\t%c%c%c%c%c%c%c%c%c%c%c-- %d %d %d %d %d\n",
  80.                     c->sunday_first ? 's' : '-',
  81.                     c->ampm         ? 'a' : '-',
  82.                     c->mmddyy       ? 'm' : '-',
  83.                     c->autodel      ? 'd' : '-',
  84.                     c->julian       ? 'j' : '-',
  85.                     c->weeknum      ? 'w' : '-',
  86.                     c->nopast       ? 'n' : '-',
  87.                     c->bigwarning   ? 'b' : '-',
  88.                     c->weekwarn     ? 'w' : '-',
  89.                     c->weekuser     ? 'u' : '-',
  90.                     c->smallmonth   ? 's' : '-',
  91.                     c->early_time,
  92.                     c->late_time,
  93.                     (int)c->wintimeout,
  94.                     c->week_minhour,
  95.                     c->week_maxhour);
  96.  
  97.         fprintf(fp, "t\t%d %d %d %d %d\n",
  98.                     (int)c->adjust_time,
  99.                     (int)c->raw_tzone,
  100.                     c->dst_flag,
  101.                     c->dst_begin,
  102.                     c->dst_end);
  103.  
  104.         fprintf(fp, "e\t%c%c%c %s\n",
  105.                     c->ewarn_window ? 'w' : '-',
  106.                     c->ewarn_mail   ? 'm' : '-',
  107.                     c->ewarn_exec   ? 'x' : '-',
  108.                     c->ewarn_prog   ? c->ewarn_prog
  109.                                 : "");
  110.         fprintf(fp, "l\t%c%c%c %s\n",
  111.                     c->lwarn_window ? 'w' : '-',
  112.                     c->lwarn_mail   ? 'm' : '-',
  113.                     c->lwarn_exec   ? 'x' : '-',
  114.                     c->lwarn_prog   ? c->lwarn_prog
  115.                                 : "");
  116.         fprintf(fp, "a\t%c%c%c %s\n",
  117.                     c->alarm_window ? 'w' : '-',
  118.                     c->alarm_mail   ? 'm' : '-',
  119.                     c->alarm_exec   ? 'x' : '-',
  120.                     c->alarm_prog   ? c->alarm_prog
  121.                                 : "");
  122.         if (*Print_Spooler)
  123.             fprintf(fp, "p\t%s\n", Print_Spooler);
  124.  
  125.         if (c->mailer)
  126.             fprintf(fp, "m\t%s\n", c->mailer);
  127.  
  128.         fprintf(fp, "U\t%d\n", nusers);
  129.         for (i=0; i < nusers; i++)
  130.             fprintf(fp, "u\t%s %s %d %d\n",
  131.                     user[i].name,
  132.                     user[i].home,
  133.                     user[i].suspended,
  134.                     user[i].color);
  135.     }
  136.     for (ep=list->entry, i=0; i < list->nentries; i++, ep++) {
  137.         if (ep->private && !(what & WR_PRIVATE) ||
  138.            !ep->private && !(what & WR_PUBLIC))
  139.             continue;
  140.         tm = time_to_tm(ep->time);
  141.         fprintf(fp,
  142.           "%d/%d/%d  %d:%d:%d  %d:%d:%d  %d:%d:%d  %d:%d:%d  %c%c%c\n",
  143.             (int)tm->tm_mon+1,
  144.             (int)tm->tm_mday,
  145.             (int)tm->tm_year + 1900,
  146.             (int)(ep->notime ? 99 : tm->tm_hour),
  147.             (int)(ep->notime ? 99 : tm->tm_min),
  148.             (int)(ep->notime ? 99 : tm->tm_sec),
  149.             (int)ep->length     / 3600,
  150.             (int)ep->length     / 60   % 60,
  151.             (int)ep->length            % 60,
  152.             (int)ep->early_warn / 3600,
  153.             (int)ep->early_warn / 60   % 60,
  154.             (int)ep->early_warn        % 60,
  155.             (int)ep->late_warn  / 3600,
  156.             (int)ep->late_warn  / 60   % 60,
  157.             (int)ep->late_warn         % 60,
  158.             (int)ep->suspended ? 'S' : '-',
  159.             (int)ep->private   ? 'P' : '-',
  160.             (int)ep->noalarm   ? 'N' : '-');
  161.  
  162.         if (ep->rep_every || ep->rep_weekdays || ep->rep_days
  163.                               || ep->rep_yearly)
  164.             fprintf(fp, "R\t%d %d %d %d %d\n",
  165.                 (int)ep->rep_every,
  166.                 (int)ep->rep_last,
  167.                 (int)ep->rep_weekdays,
  168.                 (int)ep->rep_days,
  169.                 (int)ep->rep_yearly);
  170.  
  171.         if (ep->note)
  172.             for (p=ep->note; *p; ) {
  173.                 for (q=line; *p && *p != '\n'; )
  174.                     *q++ = *p++;
  175.                 *q = 0;
  176.                 fprintf(fp, "N\t%s\n", line);
  177.                 if (*p) p++;
  178.             }
  179.  
  180.         if (ep->message)
  181.             for (p=ep->message; *p; ) {
  182.                 for (q=line; *p && *p != '\n'; )
  183.                     *q++ = *p++;
  184.                 *q = 0;
  185.                 fprintf(fp, "M\t%s\n", line);
  186.                 if (*p) p++;
  187.             }
  188.  
  189.         if (ep->script)
  190.             for (p=ep->script; *p; ) {
  191.                 for (q=line; *p && *p != '\n'; )
  192.                     *q++ = *p++;
  193.                 *q = 0;
  194.                 fprintf(fp, "S\t%s\n", line);
  195.                 if (*p) p++;
  196.             }
  197.  
  198.         if (ep->meeting)
  199.             for (p=ep->meeting; *p; ) {
  200.                 for (q=line; *p && *p != '\n'; )
  201.                     *q++ = *p++;
  202.                 *q = 0;
  203.                 fprintf(fp, "G\t%s\n", line);
  204.                 if (*p) p++;
  205.             }
  206.     }
  207.     lockfile(fp, FALSE);
  208.     fclose(fp);
  209.     list->modified = FALSE;
  210.     list->locked   = FALSE;
  211.     return(TRUE);
  212. }
  213.